/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.cib.fintech.entrustedinvestment.biz.service.impl;

import com.cib.fintech.entrustedinvestment.biz.domain.Tenant;
import com.cib.fintech.entrustedinvestment.utils.ValidationUtil;
import com.cib.fintech.entrustedinvestment.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.cib.fintech.entrustedinvestment.biz.repository.TenantRepository;
import com.cib.fintech.entrustedinvestment.biz.service.TenantService;
import com.cib.fintech.entrustedinvestment.biz.service.dto.TenantDto;
import com.cib.fintech.entrustedinvestment.biz.service.dto.TenantQueryCriteria;
import com.cib.fintech.entrustedinvestment.biz.service.mapstruct.TenantMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.cib.fintech.entrustedinvestment.utils.PageUtil;
import com.cib.fintech.entrustedinvestment.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author zhangqingkun
* @date 2020-10-16
**/
@Service
@RequiredArgsConstructor
public class TenantServiceImpl implements TenantService {

    private final TenantRepository tenantRepository;
    private final TenantMapper tenantMapper;

    @Override
    public Map<String,Object> queryAll(TenantQueryCriteria criteria, Pageable pageable){
        Page<Tenant> page = tenantRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(tenantMapper::toDto));
    }

    @Override
    public List<TenantDto> queryAll(TenantQueryCriteria criteria){
        return tenantMapper.toDto(tenantRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TenantDto findById(Long id) {
        Tenant tenant = tenantRepository.findById(id).orElseGet(Tenant::new);
        ValidationUtil.isNull(tenant.getId(),"Tenant","id",id);
        return tenantMapper.toDto(tenant);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TenantDto create(Tenant resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return tenantMapper.toDto(tenantRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Tenant resources) {
        Tenant tenant = tenantRepository.findById(resources.getId()).orElseGet(Tenant::new);
        ValidationUtil.isNull( tenant.getId(),"Tenant","id",resources.getId());
        tenant.copy(resources);
        tenantRepository.save(tenant);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            tenantRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<TenantDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TenantDto tenant : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("租户名称", tenant.getName());
            map.put("租户id", tenant.getTenId());
            map.put("平台RSA公钥", tenant.getRsaPub());
            map.put("平台RSA私钥", tenant.getRsaPri());
            map.put("租户RSA公钥", tenant.getTenRsaPub());
            map.put("租户异步通知地址1", tenant.getReturnUrl1());
            map.put("租户异步通知地址2", tenant.getReturnUrl2());
            map.put("租户异步通知地址3", tenant.getReturnUrl3());
            map.put("租户异步通知地址4", tenant.getReturnUrl4());
            map.put("租户异步通知地址5", tenant.getReturnUrl5());
            map.put("备注", tenant.getRemark());
            map.put("租户状态0无效1有效", tenant.getStatus());
            map.put("创建时间", tenant.getCreateTime());
            map.put("创建人", tenant.getCreateBy());
            map.put("更新时间", tenant.getUpdateTime());
            map.put("跟新人", tenant.getUpdateBy());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}